﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dln.Common;
using Dln.Common.Helper;
using Dln.Model;


public partial class Page_AutoRun_SendProblem : System.Web.UI.Page
{
    protected dt_order dtorderModel = new dt_order();
    protected string cardId = string.Empty;
    protected int Id = 0;
    protected int ProblemState = 0;
    protected string State = "";
    protected int typeVal = 0;
    protected string Jieshouren = "无";
    protected int StarNum = 0;
    protected void Page_Load(object sender, EventArgs e)
    {

        Id = Convert.ToInt32(HttpContext.Current.Request.QueryString["Id"]);
        cardId = HttpContext.Current.Request.QueryString["cardId"];

        dtorderModel = SqlHelper.ContextDlnOnline.From<dt_order>().Where(m => m.id == Id).ToFirst();
        if (dtorderModel != null)
        {
            ProblemState = (int)dtorderModel.state;
            StarNum = string.IsNullOrEmpty(dtorderModel.starNum.ToString()) ? 0 : (int)dtorderModel.starNum;
            if (dtorderModel.state == 0)
            {
                State = "待处理";
            }
            else if (dtorderModel.state == 1)
            {
                State = "处理中";
            }
            else if (dtorderModel.state == 2)
            {
                State = "已驳回";
            }
            else if (dtorderModel.state == 4)
            {
                State = "已处理";
            }
            else if (dtorderModel.state == 3)
            {
                State = "已完成";
            }
            typeVal = Convert.ToInt32(dtorderModel.type);

            if (!string.IsNullOrEmpty(dtorderModel.update_name))
            {
                var managerModel =
                    SqlHelper.ContextDlnOnline.From<dt_manager>()
                        .Where(m => m.user_name == dtorderModel.update_name)
                        .First();
                if (managerModel != null)
                    Jieshouren = managerModel.real_name + (string.IsNullOrEmpty(managerModel.telephone)
                        ? ""
                        : " || " + managerModel.telephone);
            }
        }

        TreeBind();
    }

    /// <summary>
    /// 取得所有类别列表
    /// </summary>
    /// <param name="parent_id">父ID</param>
    /// <param name="channel_id">频道ID</param>
    /// <returns></returns>
    public DataTable GetList(int parent_id)
    {
        DataTable oldData = SqlHelper.ContextDlnOnline.FromSql("select * from dt_type_list where class_layer !=''").ToDataTable();
        if (oldData == null || oldData.Rows.Count <= 0)
        {
            return null;
        }

        //复制结构
        DataTable newData = oldData.Clone();
        //调用迭代组合成DAGATABLE
        GetChilds(oldData, newData, parent_id);
        return newData;
    }
    /// <summary>
    /// 从内存中取得所有下级类别列表（自身迭代）
    /// </summary>
    private void GetChilds(DataTable oldData, DataTable newData, int parent_id)
    {
        DataRow[] dr = oldData.Select("parentID='" + parent_id.ToString() + "'");
        for (int i = 0; i < dr.Length; i++)
        {
            DataRow row = newData.NewRow();//创建新行
            //循环查找列数量赋值
            for (int j = 0; j < dr[i].Table.Columns.Count; j++)
            {
                row[dr[i].Table.Columns[j].ColumnName] = dr[i][dr[i].Table.Columns[j].ColumnName];
            }
            newData.Rows.Add(row);
            //调用自身迭代
            this.GetChilds(oldData, newData, int.Parse(dr[i]["id"].ToString()));
        }
    }

    #region 绑定类别=================================
    private void TreeBind()
    {
        //this.leibie.DataSource = a;
        //this.leibie.DataTextField = "TypeName";
        //this.leibie.DataValueField = "Id";
        //this.leibie.DataBind();

        DataTable dt = GetList(0);
        this.type.Items.Clear();
        this.type.Items.Add(new ListItem("请选择问题类别", "0"));
        foreach (DataRow dr in dt.Rows)
        {
            string Id = dr["id"].ToString();
            int ClassLayer = Convert.ToInt32(dr["class_layer"].ToString());
            string Title = dr["typeName"].ToString().Trim();

            if (ClassLayer == 1)
            {
                this.type.Items.Add(new ListItem(Title, Id));
            }
            else
            {
                Title = "├ " + Title;
                Title = StringOfChar(ClassLayer - 1, "　") + Title;
                this.type.Items.Add(new ListItem(Title, Id));
            }
        }
    }
    #endregion

    #region 生成指定长度的字符串
    /// <summary>
    /// 生成指定长度的字符串,即生成strLong个str字符串
    /// </summary>
    /// <param name="strLong">生成的长度</param>
    /// <param name="str">以str生成字符串</param>
    /// <returns></returns>
    public string StringOfChar(int strLong, string str)
    {
        string ReturnStr = "";
        for (int i = 0; i < strLong; i++)
        {
            ReturnStr += str;
        }

        return ReturnStr;
    }
    #endregion
}